package com.ghbank.rdws.assetsservice.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ghbank.rdws.assetsservice.dto.AccountDTO;
import com.ghbank.rdws.assetsservice.dto.PartyDTO;
import com.ghbank.rdws.biz.enums.FetchName;
import com.ghbank.rdws.biz.service.RdwsService;
import com.ghbank.rdws.common.http.GhbankResponseEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "收益总额控制器")
@RestController
@Slf4j
public class IncomeTotalController {
    @Autowired
    private RdwsService rdwsService;

    @ApiOperation(value = "收益总额", notes = "根据客户号获取收益总额")
    @ApiImplicitParam(name = "partyDTO",value = "客户号",required = true,dataType = "PartyDTO")
    @PostMapping(value = "/assets/income_total")
    public GhbankResponseEntity<Map<String, Object>> getIncomeTotal(@Valid @RequestBody PartyDTO partyDTO) {
        Map<String, Object> result = new HashMap<String, Object>();
        String ptyId = partyDTO.getPtyId();
        String seq = partyDTO.getSeq();
        String token = partyDTO.getToken();
        List<AccountDTO> accounts = partyDTO.getAccounts();
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("ptyId", ptyId);
            List<String> acctnos = new ArrayList<String>();
            for (AccountDTO accountDTO:accounts) {
                acctnos.add(accountDTO.getAcctNo());
            }
            paramMap.put("acctnos", acctnos);
            result = rdwsService.queryForMap(FetchName.INCOME_TOTAL.getName(), paramMap);
            return GhbankResponseEntity.ok(result, seq);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return GhbankResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), seq);
        }
    }
}
